import { QL } from 'iflux2'
import { Map } from 'immutable'
const transform2Tree = (section) => {
    return Map({ 
        key: section.get('id'),
        value: String(section.get('id')),
        label:section.get('sname'),
    });
}

/**
 * 顶层系统栏目筛选
 */
export const topSectionsQL = QL("topSectionsQL",[
    'sections',
    (sections)=>sections.filter(item=>item.get('pid') == 0).map(it => transform2Tree(it))
]);

/**
 * 组合ql，包含子栏目的合并到topsections的children字段里
 * @type {iflux2.QueryLang}
 */
export const treeSectionsQL = QL("treeSectionsQL",[
    topSectionsQL,
    'sections',
    (topSectionsQL,sections)=>{
        let subSections = sections.filter(item => item.get('pid') != 0).groupBy(section =>section.get('pid'));
        return topSectionsQL.map(sc => {
            if(subSections.has(sc.get('key'))){
                return sc.set('children',subSections.get(sc.get('key')).map((sub)=>transform2Tree(sub)));
            }
            return sc;
        })
    }
]);
